cmake_minimum_required(VERSION 3.6)
project(json
	VERSION 0.1.0
	# DESCRIPTION "Lua json module"
	# HOMEPAGE_URL "www.zhyingkun.com"
	LANGUAGES C CXX
)

if(NOT CMAKE_BUILD_TYPE)
	set(CMAKE_BUILD_TYPE "Debug")
endif()
# message(STATUS "CMakeLists.txt for ${PROJECT_NAME}")
# message(STATUS "CMAKE_BUILD_TYPE is ${CMAKE_BUILD_TYPE}")

# LUA_BUILD_AS_DLL are for all windows components, include liblua, cmod library, and user exe which use liblua
if(APPLE)
	set(CMAKE_C_FLAGS         "-std=gnu99 -fPIC -Wall")
	set(CMAKE_C_FLAGS_DEBUG   "-g")
	set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
	set(CMAKE_C_FLAGS         "-std=gnu99 -fPIC -Wall")
	set(CMAKE_C_FLAGS_DEBUG   "-g")
	set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
elseif(WIN32)
	set(CMAKE_C_FLAGS         "/DLUA_BUILD_AS_DLL") # /Wall
	set(CMAKE_C_FLAGS_DEBUG   "/ZI /Od")
	set(CMAKE_C_FLAGS_RELEASE "/O2 /DNDEBUG")
endif()

include_directories(../../liblua/include)
include_directories(../../liblua/core)
include_directories(./src)
include_directories(./include)

aux_source_directory(./src JSONMOD_SRC)
file(GLOB JSONMOD_HEADERS "./src/*.h")
source_group(src FILES ${JSONMOD_SRC} ${JSONMOD_HEADERS})

file(GLOB JSON_HEADERS "./include/*.h")
source_group(include FILES ${JSON_HEADERS})

aux_source_directory(./wrap WRAP_SRC)
source_group(wrap FILES ${WRAP_SRC})

set(JSON_FILES ${JSONMOD_SRC} ${JSONMOD_HEADERS} ${JSON_HEADERS})
# add_library(json_static STATIC ${JSON_FILES})
# set_target_properties(json_static PROPERTIES FOLDER "cmod/json")

# dynamic load library  .so .bundle
add_library(${PROJECT_NAME} MODULE ${JSON_FILES} ${WRAP_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES
	FOLDER "cmod"
	# OUTPUT_NAME ${PROJECT_NAME}
	# VERSION "0.1.0"
	# SOVERSION "0.1.0"
	INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib
	POSITION_INDEPENDENT_CODE ON
)
target_link_libraries(${PROJECT_NAME} liblua)
if(WIN32)
	set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "lib")
endif(WIN32)

install(TARGETS ${PROJECT_NAME}
	RUNTIME DESTINATION bin
	LIBRARY DESTINATION cmods/5.3
	ARCHIVE DESTINATION cmods/5.3
)

install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION runner/cmods)
